package de.gbeine.java.depview.beans;

import javax.swing.JFileChooser;

import lombok.extern.slf4j.Slf4j;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

import de.gbeine.java.depview.event.OpenFileEvent;
import de.gbeine.java.depview.event.ParseFileEvent;

/**
 *
 */
@Slf4j
public final class OpenFile {

    /**
     * The event bus to publish events.
     */
    private final EventBus eventBus;

    /**
     * Creates a new OpenFile bean.
     * @param pEventBus The EventBus to sent messages to.
     */
    public OpenFile(final EventBus pEventBus) {
        super();
        this.eventBus = pEventBus;
    }

    /**
     *
     * @param pOpenFileEvent
     */
    @Subscribe
    public void onOpenFileEvent(final OpenFileEvent pOpenFileEvent) {
        final JFileChooser fileChooser = new JFileChooser(".");
        int status = fileChooser.showOpenDialog(null);
        if (JFileChooser.APPROVE_OPTION == status) {
            OpenFile.log.debug("Opening file {}", fileChooser.getSelectedFile().getName());
            ParseFileEvent event = new ParseFileEvent(this, fileChooser.getSelectedFile());
            this.eventBus.post(event);
        }
    }
}
